package com.kafka.core;

import java.util.Iterator;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kafka.bean.Message;
import com.kafka.common.String.StringValues;
import com.kafka.dao.DeviceMapper;
import com.kafka.dao.IccardMapper;
import com.kafka.dao.MessageMapper;
import com.kafka.dao.UserMapper;
import com.kafka.enumtype.ResponseType;
import com.kafka.util.BaseUtil;
import com.kafka.util.DbUtil;

/**
 * producer基类
 * 
 * @author leihongan
 * 
 */
public class BaseClass {
	protected static SqlSession sqlSession = DbUtil.getSessionFactory().openSession(true);
	public static IccardMapper iccardDao = sqlSession.getMapper(IccardMapper.class);
	public static UserMapper userDao = sqlSession.getMapper(UserMapper.class);
	public static DeviceMapper deviceDao = sqlSession.getMapper(DeviceMapper.class);
	protected MessageMapper messageDao = sqlSession.getMapper(MessageMapper.class);
	public static Logger log = LoggerFactory.getLogger(BaseClass.class);


	/**
	 * 消息发送成功提示
	 * 
	 * @param msgId
	 * @return
	 */
	protected static String successMsg(Object msgId) {
		JSONObject rs = new JSONObject();
		rs.put("success", true);
		rs.put("msg", StringValues.SEND_SUCCESSFUL);
		rs.put("msgid", msgId);
		rs.put("code", ResponseType.REQUEST_SUCCESSFUL.toString());
		log.info("###producer:" + rs.toString() + ",time:" + BaseUtil.getDate());
		return rs.toString();
	}
	
	/**
	 * 消息发送成功提示
	 * @param msgId
	 * @return
	 */
	protected static String successMsg(JSONObject jsonMsg, String msg) throws Exception{
		JSONObject rs = new JSONObject();
		rs.put("success", true);
		rs.put("msg", msg);
		log.info("###producer:"+String.valueOf(rs)+",time:" + BaseUtil.getDate());
		return rs.toString();
	}
	
	/**
	 * 查询成功响应信息
	 * @param msgId
	 * @param datas
	 * @return
	 */
	public static String successMsg(Object msgId,JSONObject datas) {
		JSONObject rs = new JSONObject();
		rs.put("success", true);
		rs.put("msg", StringValues.QUERY_SUCCESSFUL);
		rs.put("msgid", msgId);
		rs.put("datas", datas.toString());
		rs.put("code", ResponseType.REQUEST_SUCCESSFUL.toString());
		log.info("###QueryResource:" + rs.toString() + ",time:" + BaseUtil.getDate());
		return rs.toString();
	}
	
	/**
	 * 消息发送失败提示
	 * 
	 * @param msgId
	 * @return
	 */
	protected static String failMsg(Object msgId, String msg,String code) {
		JSONObject rs = new JSONObject();
		rs.put("success", false);
		rs.put("msg", msg);
		rs.put("msgid", msgId);
		rs.put("code", code);
		log.info("###producer:" + rs.toString() + ",time:" + BaseUtil.getDate());
		return rs.toString();
	}
	

	protected static JSONObject failMsg(JSONObject result,String msg,String responseType, String msgId) {
		result.put("success", false);
		result.put("msg", msg);
		result.put("code", responseType);
		result.put("msgid", msgId);
		return result;
	}
	
	/**
	 * 发送返回json信息
	 * 
	 * @param jsonMsg
	 * @param msg
	 * @return
	 */
	protected static String failMsg(JSONObject jsonMsg, String msg) {
		JSONObject error = new JSONObject();
		error.put("success", false);
		error.put("msg", msg);
		try {
			error.put("msgid", jsonMsg.get("msgid"));
		} catch (Exception e) {
			error.put("msgid", e.toString());
		}
		log.info("###message:" + String.valueOf(error) + ",time:" + BaseUtil.getDate());
		return error.toString();
	}
	

	/**
	 * 消息发送失败提示
	 * 
	 * @param msgId
	 * @return
	 */
	protected static String failMsg(Object msgId, String msg) {
		JSONObject rs = new JSONObject();
		rs.put("success", false);
		rs.put("msg", msg);
		rs.put("msgid", msgId);
		rs.put("code", ResponseType.SYSTEM_ERROR.toString());
		log.info("###producer:" + rs.toString() + ",time:" + BaseUtil.getDate());
		return rs.toString();
	}

	/**
	 * 添加数据库日志记录
	 * @param message
	 * @param topic
	 * @param status
	 * @param description
	 */
	protected void addMessage(JSONObject message,String topic,
			Integer status, String description){
		Message msg = new Message();
		msg.setTopic(topic);
		msg.setMessage(String.valueOf(message));
		msg.setMessageType(Integer.valueOf(message.getString("msgtype")));
		msg.setCreateTime(message.getString("createtime"));
		msg.setTime(BaseUtil.getDate());
		msg.setStatus(status);
		msg.setDescription(description);
		msg.setMessageId(message.getString("msgid"));
		messageDao.insertMessageInfo(msg);
		sqlSession.commit();
	}
	
	/**
	 * 重构json
	 * @param json
	 * @return
	 */
	protected static JSONObject rebuildJSON(JSONObject json){
		JSONObject rJson = new JSONObject();
		Iterator<?> it = json.keys();
        while (it.hasNext()) {  
            String key = (String) it.next();  
            Object value = json.get(key); 
            rJson.put(BaseUtil.strToLower(key), value);
        }
        return rJson;
	}
	
	/**
	 * 重构jsonArray
	 * @param jsonArray
	 * @return
	 */
	protected static JSONArray rebuildJSONArray(JSONArray jsonArray){
		JSONArray rJsonArray = new JSONArray();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject obj = JSONObject.fromObject(jsonArray.get(i));
			rJsonArray.add(rebuildJSON(obj));
		}
		return rJsonArray;
	}

}
